import 'dart:ui' as ui;

import 'package:common_utils/common_utils.dart';
import 'package:fate_blind_box/chat_view/chat_net_image.dart';
import 'package:fate_blind_box/config/app_style.dart';
import 'package:fate_blind_box/theme/theme_color.dart';
import 'package:flutter/material.dart';
import 'package:flutter_quill_extensions/shims/dart_ui_real.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:im_flutter_sdk/im_flutter_sdk.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'logic.dart';

class ChatMessageListPage extends StatefulWidget {
  const ChatMessageListPage({super.key});

  @override
  State<ChatMessageListPage> createState() => _ChatMessageListPageState();
}

class _ChatMessageListPageState extends State<ChatMessageListPage> {
  final controller = Get.find<ChatMessageListLogic>();
  final state = Get.find<ChatMessageListLogic>().state;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  void didUpdateWidget(covariant ChatMessageListPage oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    EMClient.getInstance.chatManager
        .removeEventHandler("YXA6hl6vye2MTQOb2k-1mAKAiA");
  }

  @override
  void deactivate() {
    // TODO: implement deactivate
    super.deactivate();
    EMClient.getInstance.chatManager
        .removeEventHandler("YXA6hl6vye2MTQOb2k-1mAKAiA");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        centerTitle: true,
        title: Text(
          '消息',
          style: TextStyle(
              fontSize: 18.sp,
              fontWeight: FontWeight.w700,
              color: Colors.black),
        ),
        flexibleSpace: Container(
          decoration: BoxDecoration(
              gradient: LinearGradient(
                  colors: [ThemeColor.color19CF8F23, ThemeColor.color19CF8F23],
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight)),
        ),
      ),
      backgroundColor: "#F5F5F5".stringToColor,
      body: Obx(() {
        return SmartRefresher(
            controller: controller.state.refreshController,
            enablePullDown: true,
            enablePullUp: false,
            onRefresh: controller.onRefresh,
            child: controller.state.model.isEmpty
                ? emptyOrderIteam()
                : hasOrderIteam());
      }),
    );
  }

  Widget emptyOrderIteam() {
    return SizedBox(
      height: ScreenUtil().screenHeight,
      width: ScreenUtil().screenWidth,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Center(
            child: Image.asset(
              'images/no_data.png',
              width: 175,
              height: 122,
            ),
          ),
        ],
      ),
    );
  }

  Widget hasOrderIteam() {
    return ListView.separated(
      itemBuilder: (context, index) => iteamContent(context, index),
      itemCount: controller.state.model.length,
      separatorBuilder: (BuildContext context, int index) {
        return Container(
          width: double.infinity,
          height: 8.w,
          color: '#EDEDED'.stringToColor,
        );
      },
    );
  }

  /// 数组cell
  Widget iteamContent(BuildContext context, int index) {
    return InkWell(
      onTap: () => controller.clearBar(index),
      child: Container(
        color: Colors.white,
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 10.w),
        child: Row(
          children: [
            SizedBox(
              width: 50.w,
              height: 50.w,
              child: Stack(
                children: [
                  controller.state.model[index].xinMsg?.from == 'admin'
                      ? Image.asset(
                          'images/icon_system.png',
                          width: 50.w,
                          height: 50.w,
                        )
                      : ChatNetImage(
                          imageUrl:
                              '${controller.state.model[index].serverMeg?.photoUrl}',
                          width: 50.w,
                          height: 50.w,
                          fit: BoxFit.cover,
                          radius: 6.w,
                        ),
                  ClipRRect(
                    borderRadius: BorderRadius.circular(5.w),
                    child: BackdropFilter(
                      filter: ui.ImageFilter.blur(sigmaX: 0.0, sigmaY: 0.0),
                      child: Container(
                        width: 300,
                        height: 300,
                        color: Colors.transparent, // 透明色
                      ),
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(width: 8.w),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Expanded(
                          child: Text(
                              controller.state.model[index].xinMsg?.from ==
                                      'admin'
                                  ? '互动消息'
                                  : controller.state.model[index].serverMeg
                                          ?.nickName ??
                                      '',
                              style: TextStyle(
                                  color: '#3A3A3A'.stringToColor,
                                  fontSize: 16.sp,
                                  fontWeight: FontWeight.w400))),
                      Text(controller.getServeTime(index),
                          style: TextStyle(
                            color: '#959595'.stringToColor,
                            fontSize: 12.sp,
                            fontWeight: FontWeight.w400,
                          ))
                    ],
                  ),
                  SizedBox(height: 7.w),
                  Row(
                    children: [
                      controller.state.model[index].xinMsg?.from == 'admin'
                          ? const SizedBox()
                          : personIteam(index: index),
                      SizedBox(
                          width: controller.state.model[index].xinMsg?.from ==
                                  'admin'
                              ? 0
                              : 8.w),
                      Expanded(
                        child: Text(
                          controller.getContext(
                              message: controller.state.model[index].xinMsg),
                          maxLines: 1,
                          style: TextStyle(
                            color: '#A7A7A7'.stringToColor,
                            fontSize: 12.sp,
                            fontWeight: FontWeight.w400,
                          ),
                        ),
                      ),
                      (controller.state.model[index].serverMeg?.count != null &&
                              !NumUtil.isZero(controller
                                  .state.model[index].serverMeg!.count))
                          ? Container(
                              padding: EdgeInsets.only(
                                  left: 5.w, right: 5.w, top: 1.w, bottom: 1.w),
                              decoration: BoxDecoration(
                                color: Colors.red,
                                borderRadius:
                                    BorderRadius.all(Radius.circular(10.w)),
                                border:
                                    Border.all(width: 1, color: Colors.white),
                              ),
                              child: Text(
                                '${controller.state.model[index].serverMeg?.count ?? 0}',
                                style: TextStyle(
                                    color: '#FFFFFF'.stringToColor,
                                    fontSize: 12,
                                    fontWeight: FontWeight.w600),
                              ),
                            )
                          : const SizedBox(),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 性别 年龄
  Widget personIteam({required int index}) {
    if (controller.state.model[index].serverMeg!.index! == 1 ||
        controller.state.model[index].serverMeg!.index! == 2) {
      return const SizedBox();
    }
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 8.w, vertical: 2.w),
      width: 44.w,
      height: 16.w,
      decoration: BoxDecoration(
          color: controller.state.model[index].serverMeg?.index! == 1
              ? ThemeColor.nanColor
              : controller.state.model[index].serverMeg!.index == 2
                  ? ThemeColor.mainColor
                  : const Color(0xffFE8A45),
          borderRadius: BorderRadius.circular(36.w)),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(
            controller.state.model[index].serverMeg!.index! == 1
                ? 'images/user/txl_man_icon.png'
                : controller.state.model[index].serverMeg!.index! == 2
                    ? 'images/user/txl_woman_icon.png'
                    : 'images/user/txl_ql_icon.png',
            width: 8.w,
            height: 8.w,
            fit: BoxFit.cover,
            color: Colors.white,
          ),
          SizedBox(width: 2.w),
          Text(
            '${controller.state.model[index].serverMeg?.age ?? 0}',
            style: TextStyle(
                color: '#FFFFFF'.stringToColor,
                fontSize: 10.sp,
                fontWeight: FontWeight.w400),
          ),
        ],
      ),
    );
  }
}
